/*
 * Copyright 2009 Vladimir Ritz Bossicard
 *
 * This file is part of OraDoc.
 *
 * OraDoc is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Version     : $Revision: 12 $
 * Last edit   : $Date: 2009-03-06 22:18:45 +0000 (Fri, 06 Mar 2009) $
 * Last editor : $Author: vbossica $
 */
package org.oradoc.utils.antlr;

import java.io.IOException;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;

/**
 * Specialized {@link ANTLRFileStream} for case insensitive grammars. The code
 * is directly taken from the 
 * <a href="http://www.antlr.org/wiki/pages/viewpage.action?pageId=1782">antlr
 * wiki</a>.
 * 
 * @author Vladimir Ritz Bossicard
 */
public class CaseInsensitiveFileStream extends ANTLRFileStream {

    public CaseInsensitiveFileStream(final String fileName) throws IOException {
        super(fileName, null);
    }

    public CaseInsensitiveFileStream(final String fileName, final String encoding) throws IOException {
        super(fileName, encoding);
    }

    @Override
    public int LA(int i) {
        if (i == 0) {
            return 0; // undefined
        }
        if (i < 0) {
            i++; // e.g., translate LA(-1) to use offset 0
        }
        if ((this.p + i - 1) >= this.n) {
            return CharStream.EOF;
        }
        return Character.toUpperCase(this.data[this.p + i - 1]);
    }
}
